package adke.managers;

import adke.gui.Overview;
import adke.listeners.PlayerListener;
import adke.models.AI;
import adke.models.Player;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Vector;
import javax.swing.SwingUtilities;

public class PlayerManager
{
    private static Vector<Player> players;
    
    public PlayerManager()
    {
        players = new Vector();
    }
    
    // voeg listener toe aan alle players
    public static void addListener(PlayerListener listener)
    {
        for (Player p : players)
            p.addListener(listener);
    }
    
    public static Player newPlayer()
    {
        // als er niet genoeg inputs zijn
        if (getPlayerCount() == InputManager.getMaximumInputs())
        {
            System.out.println("Maximum bereikt. Er zijn niet genoeg keyboard inputs gedefinieerd");
            return null;
        }
        
        Player player = new Player();
        players.add(player);
        
        return player;
    }
    
    public static AI newAI()
    {
        // geen input check nodig
        
        AI player = new AI();
        players.add(player);
        
        return player;
    }
    
    public static void removePlayer(Player player)
    {
        removePlayer(player.getName());
    }
    
    public static void removePlayer(String playerName)
    {
        for (Player p : players)
        {
            if (p.getName().equals(playerName))
            {
                players.remove(p);
                
                // geef eigenschappen vrij
                InputManager.returnControls(p.getControls());
                ColorManager.returnColor(p.getColor());
                
                return;
            }
        }
    }
    
    public static int getPlayerCount()
    {
        players.trimToSize();
        return players.size();
    }
    
    public static int getPlayerCount(boolean alive)
    {
        int cnt = 0;
        for (Player p : players)
        {
            if (p.isAlive() == alive)
            {
                cnt++;
            }
        }
        return cnt;
    }
    
    public static Player[] getPlayers()
    {
        return players.toArray(new Player[getPlayerCount()]);
    }
    
    public static Player[] getPlayers(boolean alive)
    {
        Player[] arr = new Player[getPlayerCount(alive)];
        int i = 0;
        for (Player p : players)
        {
            if (p.isAlive() == alive)
            {
                arr[i] = p;
                i++;
            }
        }
        return arr;
    }
    
    public static Player getPlayer(Color c)
    {
        for (Player p : players)
        {
            if (p.getColor().equals(c))
                return p;
        }
                
        return null;
    }
    
    public static void reset()
    {
        Player[] p = getPlayers();
        for (Player pl : p)
            removePlayer(pl);
    }
    
    // acties die we moeten toepassen op elke player
    public static void doNextSteps()
    {
        for (Player p : players)
        {
            p.getLine().nextStep();
        }
    }
    
    public static Rectangle[] paintPlayers(Graphics g)
    {
        Rectangle[] changes = new Rectangle[getPlayerCount()];
        
        int rectWriter = 0;
        
        // TODO: geeft soms ConcurrentModificationException
        for (Player p : players)
        {
            Rectangle r = p.getLine().paint(g);
            changes[rectWriter] = r;
            rectWriter++;
        }
        
        return changes;
    }
}
